<?php
// include ImageManipulator class
require_once('../lib/ImageManipulator.php');
$arrRes=array();
if ($_FILES['fileToUpload']['error'] > 0) {
    $arrRes['thongbao'] =  "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
} else {
    // array of valid extensions
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
    // get extension of the uploaded file
    $fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
    // check if file Extension is on the list of allowed ones
    if (in_array($fileExtension, $validExtensions)) {
        $newNamePrefix = time() . '_';
        $newName = time() . '_' . $_FILES['fileToUpload']['name'];
        
        $manipulator1 = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
        
        // get width , height image
        $width  = $manipulator1->getWidth();
        $height = $manipulator1->getHeight();
      
        $new_width = 236;
        $percent = round(236*100/$width);
        $new_height = round($percent*$height/100);
        // resizing to 200x200
        
        
        $newImage1 = $manipulator1->resample($new_width, $new_height);
        // saving file to uploads folder
        $manipulator1->save('../../upload/r/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
        
        $manipulator2 = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
        // get width , height image
        $width2  = $manipulator2->getWidth();
        $height2 = $manipulator2->getHeight();
        
        //tinh size crop
        $size_crop = ($width2 > $height2) ? $height2 : $width2 ;
        
        //crop
        $centreX = round($width2 / 2);
        $centreY = round($height2 / 2);
        // our dimensions will be 600x600
        $x1 = $centreX - $size_crop/2; // 220 / 2
        $y1 = $centreY - $size_crop/2; // 220 / 2
 
        $x2 = $centreX + $size_crop/2; // 200 / 2
        $y2 = $centreY + $size_crop/2; // 130 / 2
 
        // center cropping to 200x130
        $newImage2 = $manipulator2->crop($x1, $y1, $x2, $y2);
        // saving file to uploads folder
        $url_crop = '../../upload/c/' . $newNamePrefix . $_FILES['fileToUpload']['name'];
        $url_hien = '../upload/c/' . $newNamePrefix . $_FILES['fileToUpload']['name'];
        $manipulator2->save($url_crop);
        // resize hinh crop
        $manipulator3 = new ImageManipulator($url_crop);
        $newImage3 = $manipulator3->resample(220, 220);
        $manipulator3->save('../../upload/c/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
        
        // upload hinh goc
        $destination = '../../upload/' . $newName;
        move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination);
        $arrRes['thongbao'] =  'Upload thành công';
	$arrRes['str_hinhanh']="<input type='hidden' name='url_images' value='".$destination."'>";
        $arrRes['text'] .="<img src='".$url_hien."'/>";
        
    } else {
        $arrRes['thongbao'] =  'You must upload an image...';
    }
}
echo json_encode($arrRes);